Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

contrib/database/sql: Close DB Stats goroutine on db.Close() #3025

Open
wants to merge 19 commits into
base: main
Choose a base branch
from

Conversation

mtoffl01
Copy link
Contributor

@mtoffl01 mtoffl01 commented Dec 10, 2024

What does this PR do?

Introduces a stop condition for the pollDBStats goroutine when db.Close() has been invoked by the user.

Motivation

Protecting against runaway goroutines.

Details

Background
To register a sql driver for instrumentation, a user must call sqltrace.Open, which passes our tracedConnector into the OpenDB function. This tracedConnector is used as the c driver.Connector argument to database/sql's OpenDB function, which is set on the DB.connector field.
When db.Close() is called by the user, the database/sql code checks whether the DB.connector satisfies the io.Closer interface and, if so, calls connector.Close().
Therefore, to identify when a user has called db.Close(), we can implement (and control) a tracedConnector.Close() method.

Implementation
A new tracedConnector.dbClose channel is closed when tracedConnector.Close() is invoked. The pollDBStats goroutine returns (stops) when there is activity on the dbClose channel.
In this way, we've tied the pollDBStats goroutine to the lifecycle of the db.

Next Steps

Once this approach is approved, we will do the same thing for the contrib/jackc/pgx pollPoolStats goroutine.

Reviewer's Checklist

  • Changed code has unit tests for its functionality at or near 100% coverage.
  • System-Tests covering this feature have been added and enabled with the va.b.c-dev version tag.
  • There is a benchmark for any new code, or changes to existing code.
  • If this interacts with the agent in a new way, a system test has been added.
  • Add an appropriate team label so this PR gets put in the right place for the release notes.
  • Non-trivial go.mod changes, e.g. adding new modules, are reviewed by @DataDog/dd-trace-go-guild.
  • For internal contributors, a matching PR should be created to the v2-dev branch and reviewed by @DataDog/apm-go.

Unsure? Have a question? Request a review!

@github-actions github-actions bot added the apm:ecosystem contrib/* related feature requests or bugs label Dec 10, 2024
@datadog-datadog-prod-us1
Copy link

datadog-datadog-prod-us1 bot commented Dec 10, 2024

Datadog Report

Branch report: mtoff/contrib-routines
Commit report: 29d925e
Test service: dd-trace-go

❌ 1 Failed (0 Known Flaky), 5014 Passed, 71 Skipped, 2m 44.91s Total Time

❌ Failed Tests (1)

  • TestMain - gopkg.in/DataDog/dd-trace-go.v1/contrib/database/sql - Details

    Expand for error
     Failed
     
     FAIL	gopkg.in/DataDog/dd-trace-go.v1/contrib/database/sql [build failed]
    

@pr-commenter
Copy link

pr-commenter bot commented Dec 10, 2024

Benchmarks

Benchmark execution time: 2025-01-23 21:26:08

Comparing candidate commit 7b93aa2 in PR branch mtoff/contrib-routines with baseline commit 471d723 in branch main.

Found 0 performance improvements and 0 performance regressions! Performance is the same for 59 metrics, 0 unstable metrics.

@mtoffl01 mtoffl01 force-pushed the mtoff/contrib-routines branch from 0692055 to 37b52f9 Compare December 17, 2024 01:12
Copy link
Member

@darccio darccio left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. I left some small nits.

})
}

func GetStopChan() chan struct{} {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
func GetStopChan() chan struct{} {
func StopChan() chan struct{} {

This would be a better name.

mu sync.Mutex
)

func InitStopChan() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
func InitStopChan() {
func Init() {

Just in case this package evolves and needs to initialize more things beyond the stop chan.

@kakkoyun kakkoyun self-requested a review January 17, 2025 16:18
@datadog-datadog-prod-us1
Copy link

Datadog Report

Branch report: mtoff/contrib-routines
Commit report: ebd6299
Test service: dd-trace-go

✅ 0 Failed, 5208 Passed, 72 Skipped, 2m 45.92s Total Time

@mtoffl01 mtoffl01 changed the title Allow goroutines not launched by the tracer to stop on tracer.Stop() contrib/database/sql: Close DB Stats goroutine on db.Close() Jan 24, 2025
@mtoffl01 mtoffl01 marked this pull request as ready for review January 24, 2025 16:50
@mtoffl01 mtoffl01 requested a review from a team as a code owner January 24, 2025 16:50
@mtoffl01 mtoffl01 requested a review from a team as a code owner January 24, 2025 16:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
apm:ecosystem contrib/* related feature requests or bugs
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants